#include "StdAfx.h"
#include "glObject.h"
#include "SceneManager.h"
#include "glLighting.h"


SceneManager::SceneManager(void)
{
	 s_camera=CCamera::GetInstance();
	 s_camera->m_v3CamPos=Vector3(0.0f,0.0f,6.0f);
	 s_camera->m_v3Target=Vector3(0.0f,0.0f,0.0f);
	 s_camera->m_vUp=Vector3(0.0f,1.0f,0.0f);
	 s_camera->m_fNear=0.1f;
	 s_camera->m_fFar=1000.0f;
	 s_camera->m_fSpeed=0.5f;
	 light=glLighting::GetInstance();
	 light->Light.spotDirection=Vector3(0.0f,50.0f,1000.0f);
	 light->Light.ambient=Vector4(0.9f, 0.9f, 0.9f, 1.0f);
	 light->Light.diffuse=Vector4(0.5f, 0.5f, 0.5f, 1.0f);	 
	 light->Light.specular=Vector4(1.0f, 1.0f, 1.0f, 1.0f);
}


SceneManager::~SceneManager(void)
{
}

void SceneManager::loadScene(){
	char*path="../Resources/Engine/SceneManager.txt";
	char*c_temp=new char[255];
	ifstream infile;
	infile.open(path);
	if(!infile){
		cout<<"error load file: "<<path;
		return;
	}
	infile>>c_temp>>num_model;
	s_Object=new Scene_Object[num_model];
	for(int i=0;i<num_model;i++){
		infile>>c_temp>>c_temp;
		infile>>c_temp>>s_Object[i].s_model;
		infile>>c_temp>>s_Object[i].s_TexTuresCount;
		s_Object[i].s_texture=new int[s_Object[i].s_TexTuresCount];
		for(int j=0;j<s_Object[i].s_TexTuresCount;j++){
			infile>>c_temp>>s_Object[i].s_texture[j];
		}
		infile>>c_temp>>s_Object[i].s_cubeTextureCount;
		if(s_Object[i].s_cubeTextureCount){
			s_Object[i].s_cubeTexture=new int[s_Object[i].s_cubeTextureCount];
			for(int j=0;j<s_Object[i].s_cubeTextureCount;j++){
				infile>>c_temp>>s_Object[i].s_cubeTexture[j];
			}
		}
		infile>>c_temp>>s_Object[i].s_shader;
		infile>>c_temp;
		if(!strcmp(c_temp,"POSITION"))
		{
			infile>>s_Object[i].s_position.x>>s_Object[i].s_position.y>>s_Object[i].s_position.z;
		}
		infile>>c_temp;
		if(!strcmp(c_temp,"ROTATION"))
		{
			infile>>s_Object[i].s_rotation.x>>s_Object[i].s_rotation.y>>s_Object[i].s_rotation.z;
		}
		infile>>c_temp;
		if(!strcmp(c_temp,"SCALE"))
		{
			infile>>s_Object[i].s_scale.x>>s_Object[i].s_scale.y>>s_Object[i].s_scale.z;
		}
	}
	delete c_temp;
}

void SceneManager::InitObject(glObject &object,Scene_Object s_Object){
	object.model=res_Manager.res_model[s_Object.s_model];
	if(s_Object.s_TexTuresCount){
		object.textureCount=s_Object.s_TexTuresCount;
		object.textureCubeCount=0;
		object.texture=new glTexture*[s_Object.s_TexTuresCount];
		for(int i=0;i<s_Object.s_TexTuresCount;i++){
			object.texture[i]=res_Manager.res_Texture[s_Object.s_texture[i]];
		}
	}else if(s_Object.s_cubeTextureCount){
		object.textureCount=0;
		object.textureCubeCount=s_Object.s_cubeTextureCount;
		object.texture=new glTexture*[s_Object.s_cubeTextureCount];
		for(int i=0;i<s_Object.s_cubeTextureCount;i++){
			object.texture[i]=res_Manager.res_Texture[s_Object.s_cubeTexture[i]];
		}
	}
	object.position=s_Object.s_position;
	object.rotation=s_Object.s_rotation;
	object.scale=s_Object.s_scale;
	object.myShaders=res_Manager.res_Shader[s_Object.s_shader];
}

void SceneManager::initScene(){
	res_Manager.LoalResource();
	loadScene();
	m_Object=new glObject[num_model];
	for(int i=0;i<num_model;i++){
		InitObject(m_Object[i],s_Object[i]);
		m_Object[i].Init();
	}
}

void SceneManager::s_Draw(){
	for(int i=0;i<num_model;i++)
	{
		m_Object[i].Draw();
	}
}

void SceneManager::update(){
	for(int i=0;i<num_model;i++){
		m_Object[i].Update();
	}
}

void SceneManager::CleanUp(){
	for(int i=0;i<num_model;i++)
	{
		delete m_Object[i].texture;
		delete m_Object[i].vboId;
		delete s_Object[i].s_texture;
		if(s_Object[i].s_cubeTextureCount)
			delete s_Object[i].s_cubeTexture;
	}
	res_Manager.CleanUp();
	delete []m_Object;
	delete []s_Object;
	light->DestroyInstance();
}
